package com.software.software.model.server;

import java.io.File;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import javax.net.ssl.SSLException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class faceSever {
    private static HashMap<String, String> map = new HashMap<>();

    static {
        map.put("api_key", "pstG2KLPabRdUgt5NSU2uU28GLI9aAH7");
        map.put("api_secret", "wEW9-RTZJug4qMVj6ZRDNYgrNs2qdVhR");
    }

    public static boolean addFace(String outer_id, String faceToken) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/addface";
        map.put("outer_id", outer_id);
        map.put("face_tokens", faceToken);
        byte[] bacd = httpUtil.post(url, map, null);
        String str = new String(bacd);
        System.out.println(str);
        if (str.indexOf("error_message") != -1) {
            return false;
        }
        return true;
    }

    public static boolean haveFaceSet(String outer_id) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail";
        map.put("outer_id", outer_id);
        byte[] bacd = httpUtil.post(url, map, null);
        String str = new String(bacd);
        System.out.println(str);
        if (str.indexOf("error_message") != -1) {
            return false;
        }
        return true;
    }

    public static boolean creatFaceSet(String outer_id) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/create";
        map.put("outer_id", outer_id);
        byte[] bacd = httpUtil.post(url, map, null);
        String str = new String(bacd);
        System.out.println(str);
        if (str.indexOf("error_message") != -1) {
            return false;
        }
        return true;
    }

    public static boolean deleteFaceSet(String outer_id) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/delete";
        map.put("outer_id", outer_id);
        map.put("check_empty", "0");
        byte[] bacd = httpUtil.post(url, map, null);
        String str = new String(bacd);
        System.out.println(str);
        if (str.indexOf("error_message") != -1) {
            return false;
        }
        return true;
    }

    public static boolean search(String faceToken, String outer_id) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/search";
        map.put("face_token", faceToken);
        map.put("outer_id", outer_id);
        byte[] bacd = httpUtil.post(url, map, null);
        String str = new String(bacd);
        System.out.println(str);
        if(str.indexOf("error_message")==-1) {//请求没有错误
            JSONObject json = JSONObject.parseObject(str);
            JSONObject thresholds=(JSONObject) json.get("thresholds");
            Double le5=thresholds.getDouble("1e-5");
            JSONArray results=(JSONArray) json.get("results");
            if(results!=null && results.size()>=1) {
                Double confidence=((JSONObject)results.get(0)).getDouble("confidence");
                if(confidence>le5) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String isface(String file) throws Exception {
//        File file1 = new File("D:\\qqfile\\qqpicture\\oldest.jpg");
//        byte[] buff = httpUtil.getBytesFromFile(file1);
        String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
//        HashMap<String, byte[]> byteMap = new HashMap<>();
//        byteMap.put("image_base64",buff);
//        map.put("image_url", "https://s2.ax1x.com/2020/01/10/l4gg4P.jpg");
//        String file1=file.substring(31);
          map.put("image_base64",file);
        byte[] bacd = httpUtil.post(url, map, null);
        String str = new String(bacd);
        System.out.println(str);
        if (str.indexOf("error_message") != -1) {
            System.out.println("发生了错误！");
            return null;
        }
        JSONObject jsonObj = JSONObject.parseObject(str);
        int num = jsonObj.getIntValue("face_num");
        if (num == 1) {
            JSONArray array = (JSONArray) jsonObj.get("faces");
            JSONObject face = (JSONObject) array.get(0);
            String faceToken = face.getString("face_token");
            return faceToken;
        }
        return null;
    }
}
